﻿using AutoMapper;
using MediatR;
using SU.Center.Domain.RBAC;
using SU.Center.Infa;

namespace SU.Center.Api.Handler.RBAC
{
    /// <summary>
    /// 添加权限
    /// </summary>
    public class InserPermissionHandler:IRequestHandler<InserPermissionReqsultDto,ApiResult>
    {
        private readonly IBaseReposiotry<Permissions> _permissionRepository;
        private readonly ILogger<InserPermissionHandler> _logger;
        private readonly IMapper _mapper;

        public InserPermissionHandler(IBaseReposiotry<Permissions> permissionRepository,
            ILogger<InserPermissionHandler> logger,
            IMapper mapper)
        {
            _permissionRepository = permissionRepository;
            _logger = logger;
            _mapper = mapper;
        }

        public async Task<ApiResult> Handle(InserPermissionReqsultDto request, CancellationToken cancellationToken)
        {
            var obj = _mapper.Map<Permissions>(request);
            await _permissionRepository.InsetAsync(obj);
            return ApiResult.Ok();
        }
    }
}

public class InserPermissionReqsultDto : IRequest<ApiResult>
{
    public string PermissionsName { get; set; }
    //// <summary>
    /// 上下级关系
    /// </summary>
    public int? ParentId { get; set; }
    /// <summary>
    /// 路由
    /// </summary>
    public string? Route { get; set; }
}
